﻿using BootstrapBlazor.Components;
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Localization;
using System.Diagnostics.CodeAnalysis;
using TrackSystem.Entities.Dtos.Channel;
using TrackSystem.UI.Server.HttpRepository.Channel;

namespace TrackSystem.UI.Server.Components.Elements
{
    public partial class ChannelPage
    {
        [Inject]
        [NotNull]
        private IStringLocalizer<ChannelDto>? Localizer { get; set; }

        /// <summary>
        /// 获得/设置 分页配置数据源
        /// </summary>
        private static IEnumerable<int> PageItemsSource => new int[] { 10, 20, 40 };

        private static string GetAvatarUrl(int id) => $"images/avatars/150-{id}.jpg";

        private static Color GetProgressColor(int count) => count switch
        {
            >= 0 and < 10 => Color.Secondary,
            >= 10 and < 20 => Color.Danger,
            >= 20 and < 40 => Color.Warning,
            >= 40 and < 50 => Color.Info,
            >= 50 and < 70 => Color.Primary,
            _ => Color.Success
        };

        [Inject]
        public IChannelHttpRepository repo { get; set; }

        [NotNull]
        private List<ChannelDto>? Items { get; set; }



        private async Task<QueryData<ChannelWithSensorsDto>> OnQueryAsync(QueryPageOptions options)
        {
            var items = await repo.GetAllWithSensors();

            // 排序
            var isSorted = false;
            if (!string.IsNullOrEmpty(options.SortName))
            {
                items = items.Sort(options.SortName, options.SortOrder);
                isSorted = true;
            }

            var total = items.Count();

            var result =   new QueryData<ChannelWithSensorsDto>()
                    {
                        Items = items.Skip((options.PageIndex - 1) * options.PageItems).Take(options.PageItems).ToList(),
                        TotalCount = total,
                        IsFiltered = true,
                        IsSorted = isSorted,
                        IsSearch = true
                    };

            return result;
        }
    }
}
